﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

using PriceLibrary.PriceService;

using VDataAdapter.EventManagement;
using VDataAdapter.BlockingQueue;
using VDataAdapter.PriceServerAdapter;

namespace VDataAdapter.LegacyTest
{
    class PSAdapterThreadCapturer : PSAdapterThread
    {
        AutoResetEvent _resetFlag;
        PriceServerObjectFile _writer;

        public PSAdapterThreadCapturer(ManualResetEvent resetEvent, CommunicationDataQueue qMain)
            : base(resetEvent, qMain)
        {           
            _resetFlag = new AutoResetEvent(true);
            _writer = new PriceServerObjectFile();
        }

        protected override void GetFirstTime(IDictionary<string, PriceLibrary.PriceService.StockBase> baseprices, IDictionary<string, PriceLibrary.PriceService.StockBase> top3Prices)
        {
            _resetFlag.WaitOne();
            
            this.ClearStrQueue();    
            base.GetFirstTime(baseprices, top3Prices);
            _writer.WriteToFile("GetFirstTime.test", new Object[] { new FirstTimeEvent(baseprices, top3Prices), this.StrQueue });
            
            _resetFlag.Set();
        }

        protected override void  ProcessMarketInfo(IDictionary<int,PriceLibrary.PriceService.StockBase> marketInfos)
        {
            _resetFlag.WaitOne();
            
            this.ClearStrQueue();
 	        base.ProcessMarketInfo(marketInfos);            
            _writer.WriteToFile("ProcessMarketInfo.test", new Object[] { new MarketInfosEvent(TypeConvertor.StockBaseToMarketInfo(marketInfos)), this.StrQueue});
            
            _resetFlag.Set();
        }

        private List<PriceLibrary.ChartService.Transaction> _transList = new List<PriceLibrary.ChartService.Transaction>();

        protected override void ProcessTransactionInfo(IList<PriceLibrary.ChartService.Transaction> transactionsList, int countNumber, int totalCountNumber)
        {
            _resetFlag.WaitOne();

            this.ClearStrQueue();
            _transList.AddRange((List<PriceLibrary.ChartService.Transaction>)transactionsList);
            base.ProcessTransactionInfo(transactionsList, countNumber, totalCountNumber);

            _writer.WriteToFile("Transactions.test", new Object[] { new TransactionsEvent(transactionsList, countNumber, totalCountNumber, DateTime.Now) });

            if (countNumber == totalCountNumber)
            {  
                // Write count = 0 and totalcount = 0 because they are not important now
                // what we care is just list of transactions and output after received all of them
                _writer.WriteToFile("Transactions.test", new Object[] { new TransactionsEvent(transactionsList, countNumber, totalCountNumber, DateTime.Now), this.StrQueue });
            }

            _resetFlag.Set();
        }

        private int _ptTopPricesCount = 0;

        private string GetPtTop3PricesFileName()
        {
            _ptTopPricesCount++;

            return "PutThroughTopPrices" + "[" + _ptTopPricesCount + "].test";
        }

        protected override void ProcessPtTopPrices(IList<PriceLibrary.PriceService.StockBase> newstockbase)
        {
            _resetFlag.WaitOne();

            this.ClearStrQueue();
            base.ProcessPtTopPrices(newstockbase);
             
            List<StockInfoDyns> result = TestUtils.CastListObject<StockInfoDyns, StockBase>(newstockbase);
            _writer.WriteToFile("PutThroughTopPrices.test", new Object[] { new PtTopPricesChangedEvent(result),  this.StrQueue});

            _resetFlag.Set();
        }

        protected override void ProcessPtTransaction(IList<PriceLibrary.PriceService.StockBase> transactions)
        {
            _resetFlag.WaitOne();

            this.ClearStrQueue();
            base.ProcessPtTransaction(transactions);   
            _writer.WriteToFile("PutThroughTransactions.test", new Object[] { new PtTransactionsChangedEvent(TypeConvertor.StockBaseToTransaction(transactions)), this.StrQueue });

            _resetFlag.Set();
        }

        private int _top3pricesStockCount = 0;

        private string GetTop3PricesStockFileName()
        {
            _top3pricesStockCount++;
            return "Top3PricesStockChanged" + "[" + _top3pricesStockCount + "]" + ".test";
        }

        protected override void Top3PricesStockChanged(IDictionary<string, PriceLibrary.PriceService.StockBase> top3Prices)
        {
            _resetFlag.WaitOne();

            this.ClearStrQueue();
            base.Top3PricesStockChanged(top3Prices);
            _writer.WriteToFile("Top3PricesStockChanged.test", new Object[] { new Top3PricesEvent(top3Prices, DateTime.Now), this.StrQueue });

            _resetFlag.Set();
        }
    }
}
